import urllib
import urllib2
import bencode
import hashlib
import re
from urlparse import urlparse
from common import *

visited_torrents = set()

def torrent_hash(torrent):#return a string
    metainfo = bencode.bdecode(torrent)
    info = metainfo['info']
    return hashlib.sha1(bencode.bencode(info)).hexdigest()

regex_jb = re.compile('aid=[0-9]+')#torrent.jav-board.com
regex_jt = re.compile('value= ([0-9A-Z]+)')#jptorrent.org

# return hash if a new torrent successfully fetched 
def fetch_torrent(url):
	print 'Fetching Torrent %s'%url
	global visited_torrents 
	if 'torrent.jav-board.com' in url:
		try:
			html = urllib2.urlopen(url).read()
			m = regex_jb.search(html)
			if m == None:
				return ''
			values = {'ref' : m.group(0),
					  'submit' : 'DOWNLOAD' }
			data = urllib.urlencode(values)
			req = urllib2.Request('http://torrent.jav-board.com/downloadsex.php', data)
			req.add_header('User-Agent', 'Mozilla/5.0')
			response = urllib2.urlopen(req)
		except:
			print 'Exception@fetch_torrent(): %s'%url
			return
		torrent = response.read()
		thash = torrent_hash(torrent)
		if thash in visited_torrents:
			print 'Visited Torrent: %s' % thash
			return ''
		visited_torrents.add(thash)
		df = open('torrents/%s.torrent'%thash,'wb')
		df.write(torrent)
		df.close()
		return thash
	elif 'jptorrent.org' in url:
		try:
			html = urllib2.urlopen(url).read()
			m = regex_jt.search(html)
			if m == None:
				return ''
			values = {'ref' : m.group(1),
					  'submit' : 'Download' }
			data = urllib.urlencode(values)
			host = urlparse(url).netloc
			req = urllib2.Request('http://'+host+'/download.php', data)
			req.add_header('User-Agent', 'Mozilla/5.0')
			response = urllib2.urlopen(req)
		except:
			print 'Exception@fetch_torrent(): %s'%url
			return
		torrent = response.read()
		thash = torrent_hash(torrent)
		if thash in visited_torrents:
			print 'Visited Torrent: %s' % thash
			return ''
		visited_torrents.add(thash)
		df = open('torrents/%s.torrent'%thash,'wb')
		df.write(torrent)
		df.close()
		return thash
	else:
		#
		print '!!Unhandled torrent host:\n\t%s' % url
		return ''


if __name__ == "__main__":
	fetch_torrent('http://host2.jptorrent.org/link.php?ref=D5Z3Y9X0GO')
